package com.yanqu.road.server.manger.union.listener;

import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.List;

/**
 * 邮件通知-被踢或者解散
 */
public class MemberQuitMailListener implements ICrossUnionSyncListener {

    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {
        if (eUnionSyncType.BeKick.getValue() == syncType) {
            CrossUnionMember roleMember = CrossUnionMgr.getUnionMember(unionInfo.getUnionUid(), playerId);
            if(roleMember == null){
                CrossUnionMgr.getLogger().error("send {} beKick mail roleMember null", playerId);
                return;
            }
            for (CrossUnionMember member : memberList) {
                if(member.getServerId() != GameServer.getInstance().getServerId()){
                    continue;
                }
                String mailTitle = MultipleLanguageMgr.getContent(MailManager.UNION_BE_KICK_TITLE, UserMgr.getLanguage(member.getUserId()));
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.UNION_BE_KICK_CONTENT, UserMgr.getLanguage(member.getUserId())),
                        roleMember.getUserBaseInfo().getNickName(), member.getLose(), member.getContribution());
                MailManager.sendMail(member.getUserId(), eMailType.SYSTEM.getValue(), "", mailContent, mailTitle);
            }
        } else if (eUnionSyncType.DeleteUnion.getValue() == syncType) {
            for (CrossUnionMember member : memberList) {
                if(member.getServerId() != GameServer.getInstance().getServerId()){
                    continue;
                }
                String mailTitle = MultipleLanguageMgr.getContent(MailManager.UNION_DELETE_TITLE, UserMgr.getLanguage(member.getUserId()));
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.UNION_DELETE_CONTENT, UserMgr.getLanguage(member.getUserId())),
                        member.getLose(), member.getContribution());
                MailManager.sendMail(member.getUserId(), eMailType.SYSTEM.getValue(), "", mailContent, mailTitle);
            }
        }
    }
}
